home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / HyperCard Related / XCMDs & XFCNs / GetVol and NewFormat / NewFormat.c < prev   
Encoding:
C/C++ Source or Header  |  1990-10-01  |  5.2 KB  |  159 lines  |  [TEXT/MPS ]

  1. /*
  2. **        Newformat
  3. **            a REALLY simple XFNC to see if a stack is hypercard 2.0 format or not.
  4. **            Just take a look at the 20th byte in the file and see if it is 10.
  5. **            See, toldya it was simple...
  6. **
  7. **                            By Neil Day     9/24/90
  8. **                            Electronic Media Group
  9. **                            © 1990 Apple Computer, Inc.
  10. **
  11. **            Compile and link with the following MPW commands
  12. **
  13. **                            c -b newformat.c
  14. **                            link -w -rt XFCN=22503 ∂
  15. **                                -m ENTRYPOINT∂
  16. **                                -sg newformat newformat.c.o∂
  17. **                                "{libraries}HyperXLib.o" ∂
  18. **                                "{libraries}Interface.o" ∂
  19. **                                "{Clibraries}"StdCLib.o ∂
  20. **                                "{Clibraries}"CInterface.o ∂
  21. **                                "{Clibraries}"CRuntime.o ∂
  22. **                                -o "vessel"
  23. **
  24. */
  25.  
  26. #include <types.h>                                                        /* Compiler Interfaces to    */
  27. #include <resources.h>                                                    /* various managers and        */
  28. #include <files.h>                                                        /* resources ...            */
  29. #include <Packages.h>
  30. #include <String.h>
  31. #include <OSEvents.h>
  32. #include <Memory.h>
  33. #include <Events.h>
  34. #include <Errors.h>
  35. #include <Desk.h>
  36. #include <Strings.h>
  37. #include <Memory.h>
  38. #include <OSUtils.h>
  39. #include <HyperXCmd.h>
  40.  
  41. #define BUFFER_SIZE 512
  42.  
  43. HParmBlkPtr openFile (char *name,unsigned char prm);                /* Function Prototypes            */
  44. Handle str2h (char *str);
  45. void h2pstr (char *dest,Handle source);
  46.  
  47. pascal void EntryPoint (XCmdPtr paramPtr)
  48. {
  49.     HParmBlkPtr ioBlock;                                            /* paramater block for file IO    */
  50.     char fileName[256];                                                /* the name of the file to check*/
  51.     Boolean result = false;                                            /* the result of the function    */
  52.     char *bufptr;                                                    /* pointer to ioBuffer            */
  53.     char errorstr[256];
  54.     OSErr error;                                                    /* a place for errors            */
  55.     
  56.     if (paramPtr->paramCount != 1) {                                /* Wrong number of params???    */
  57.         paramPtr->returnValue = str2h ("false");                    /* return false...                */
  58.         return;                                                        /* leave the function            */
  59.     }
  60.     
  61.     h2pstr (fileName,paramPtr->params[0]);                            /* copy and Pascalize filename    */
  62.     
  63.     ioBlock = openFile (fileName,fsRdPerm);                            /* open the file for reading    */
  64.  
  65.     if (ioBlock != 0) {                                                /* if we had a happy open        */
  66.         ioBlock->ioParam.ioPosMode = fsFromStart;                    /* read from start of file...    */
  67.         ioBlock->ioParam.ioPosOffset = 0;                            /* let's start at the beginning */
  68.         ioBlock->ioParam.ioReqCount =  22;                            /* and read 18 bytes or so...    */
  69.         error = PBRead ((ParmBlkPtr)ioBlock,false);                    /* actually read it...            */
  70.         
  71.         bufptr = ioBlock->ioParam.ioBuffer;                            /* copy address of buffer         */
  72.  
  73.         if ((*(bufptr+19) == 10) && (!error))                        /* byte 16 = 10 ?? No Error???  */
  74.             result = true;                                            /* return true                    */
  75.  
  76.         DisposPtr (ioBlock->ioParam.ioBuffer);                        /* free the buffer space        */
  77.         DisposPtr ((Ptr)ioBlock);                                    /* free the space for the block    */
  78.         PBClose ((ParmBlkPtr)ioBlock,false);                        /* close that puppy up            */
  79.     }
  80.     
  81.     if (result)                                                        /* given the value of result    */
  82.         paramPtr->returnValue = str2h ("true");                        /* return true                     */
  83.     else
  84.         paramPtr->returnValue = str2h ("false");                    /* or false, depending...        */
  85.     
  86.     return;
  87. }    
  88.  
  89. /*
  90. **
  91. **        openFile
  92. **            This little beauty allocates space for a parmBlock and the associated buffer (checking
  93. **            fer errors, of course).  It then proceedes to open the file with the specified privleges
  94. **            and return error codes, if any....
  95. */
  96.  
  97. HParmBlkPtr openFile (name,prm)
  98. char *name;
  99. unsigned char prm;
  100. {
  101.     OSErr err;                                                        /* a place to put errors        */
  102.     unsigned char *buffer;                                            /* pointer to a new buffer        */
  103.     HParmBlkPtr block;                                                /* paramater block to be...        */
  104.     
  105.  
  106.     block = (HParmBlkPtr) NewPtrClear (sizeof (HParamBlockRec));    /* Allocate for a ParamBlock    */
  107.     if ((block == 0) || MemError())                                 /* if NewPtr failed, return a    */
  108.         return (HParmBlkPtr) 0;                                        /* error that thinks its a ptr    */
  109.  
  110.     buffer = NewPtrClear (BUFFER_SIZE);                                /* Allocate buffer space        */
  111.     if ((buffer == 0) || MemError())                                 /* if NewPtr failed, return a    */
  112.         return (HParmBlkPtr) 0;                                        /* error that thinks its a ptr    */
  113.     
  114.     block->fileParam.ioCompletion = NULL;                            /* no completion routine        */
  115.     block->fileParam.ioNamePtr = name;                                /* full pathname to file...     */
  116.     block->fileParam.ioVRefNum = NULL;                                /* don't need an VRefNum           */
  117.     block->fileParam.ioDirID = NULL;                                /* don't need the dirID         */
  118.     block->ioParam.ioPermssn = prm;                                    /* set the access mode          */
  119.     block->ioParam.ioMisc = NULL;                                    /* use Volume buffer...         */
  120.     block->ioParam.ioBuffer = buffer;                                /* put buffer in its place      */
  121.     err = PBHOpen (block,false);                                    /* synchronusly open file       */
  122.  
  123.     if (err) {                                                        /* if there was an error         */
  124.         DisposPtr (buffer);                                            /* free the buffer space        */
  125.         DisposPtr ((Ptr)block);                                        /* free the space for the block    */
  126.         return 0;                                                    /* return 0                     */
  127.     } else
  128.         return block;                                                /* otherwise return the block    */
  129. }
  130.     
  131. /*
  132. **    h2pstr (dest,source)
  133. **        does what it says
  134. **
  135. */
  136. void h2pstr (dest,source)
  137. char *dest;
  138. Handle source;
  139. {
  140.     strcpy ((char *) dest, *source);
  141.     dest = c2pstr (dest);
  142. }
  143.  
  144. /*
  145. **    str2h (str)
  146. **        copies a string into a handle and returns it
  147. */
  148.  
  149. Handle str2h (str)
  150. char *str;
  151. {
  152.     Handle new;
  153.     
  154.     new = NewHandle ((long) strlen(str) + 1);
  155.     strcpy ((char *) (*new), str);
  156.     
  157.     return (new);
  158. }
  159.